<?php
/*if (COUNT($_POST) > 0)
{
	$id = $this->get('id');
	if( $id == 0 )
		exit;
	$sql = "SELECT reorder FROM item_inventory WHERE iid = $id";
	Connection::getConnect();
    Connection::setQuery($sql);
	$rol = Connection::fetch();
}*/
Connection::getConnect();
if ($this->get('countreorder'))
{
	$sql = "SELECT COUNT(DISTINCT name) FROM item_details id JOIN supplier_item_cost sic USING(iid) JOIN item_inventory ii USING(iid) WHERE ii.reorder*per_unit >= ii.quantity ";
	Connection::setQuery($sql);
	$count = Connection::fetch();
	echo $count[0];
	exit;
}
if (($stat=$this->get('confirms')))
{
	$mes  = $this->get('message');
	$rotid  = $this->get('rotid');
	$sql = "UPDATE reorder_response SET status=$stat, message='$mes' WHERE rotid=$rotid";
    Connection::setQuery($sql);
	echo 1;
	exit;
}
if ($this->get('processing'))
{
	$orders  = $this->get('reorders');
	$po  = $this->get('po');
	$sdid  = $this->get('sdid');
	foreach($orders as $order){
		$sql = "INSERT INTO reorder_transaction(rotid, sdid, iid, imdid, quantity, cost, date) VALUES($po, $sdid, $order[0], $order[1], $order[2], $order[3], NOW())";
    	Connection::setQuery($sql);
		$this->logger(INVENTORY, 'Item Order - id="' . $order[0] . '" supplier="' . $sdid . '" qty="' . $order[2] . '".');
	}
	$sql = "INSERT INTO reorder_response(rotid) VALUES($po)";
    Connection::setQuery($sql);

    /*$subject="Test mail";
    $to="malufett.eat.my.binaries@gmail.com";
    $body="This is a test mail";
    if (mail($to,$subject,$body))
    	echo "Mail sent successfully!";
    else
    	echo"Mail not sent!";*/
	exit;
}

$isconfirming = $this->get('confirm');
if ($this->get('isprocessorder') || $isconfirming){
	$ispreview = $this->get('preview');
    Connection::getConnect();
	if (!$ispreview && !$isconfirming)
	{
    	$orders  = $this->get('orders');
    	$idd = $orders[0];
    	$total = 0;
    	
    	$sql = "SELECT * FROM supplier_details WHERE sdid = $idd[0]";
        Connection::setQuery($sql);
    	$supplierr = Connection::fetch();
    		
    	$sql = "SELECT MAX(rotid) as lastid, DATE_FORMAT(NOW(),'%b %d, %Y') as datenow FROM reorder_transaction";
        Connection::setQuery($sql);
    	$po = Connection::fetch();
    	if (!$po['lastid'])
    		$po_num = 5001;
    	else
    		$po_num = $po['lastid'] + 1;
	}else{
		$rotid = $this->get('rotid');
		$status = $this->get('status');
        $sql = "SELECT * FROM reorder_transaction JOIN reorder_response USING(rotid) JOIN supplier_details sd USING(sdid) WHERE rotid=$rotid";
        Connection::setQuery($sql);
        $orders = Connection::fetchAll(MYSQL_ASSOC);
		$sql = "SELECT * FROM supplier_details WHERE sdid=".$orders[0]['sdid'];
        Connection::setQuery($sql);
    	$supplierr = Connection::fetch();
    	$sql = "SELECT DATE_FORMAT(rt.date,'%b %d, %Y') as datenow FROM reorder_transaction rt WHERE rotid=$rotid";
        Connection::setQuery($sql);
    	$po = Connection::fetch();
		switch($status){
			case 1:
				foreach($orders as $order){
					$sql = "UPDATE item_inventory SET quantity=(quantity+" .$order['quantity'] . ") WHERE iid=" . $order['iid'];
        			Connection::setQuery($sql);
					$sql = "UPDATE reorder_response SET status=10 WHERE rotid=$rotid";
        			Connection::setQuery($sql);
				}
				exit;
			break;
			case 2:
				exit;
			break;
		}
	}
	function getDetail($iid, $pid, $qty)
	{
		$sql = "SELECT *,id.name as iname,sud.name as uname FROM supplier_item_cost sic
				JOIN item_details id USING(iid)
				JOIN supplier_details sd USING(sdid)
				JOIN item_unit_detail sud USING(imdid)
				WHERE sd.sdid = $pid AND id.iid = $iid";
		Connection::setQuery($sql);
		$suppl = Connection::fetch();
		echo "<tr class='list' iid='" . $suppl['iid'] . "' qty='" . $qty . "' imdid='" . $suppl['imdid'] . "' cost='" . $suppl['cost'] . "'>";
  		echo "	<td class='bg'>" . $suppl['iname'] . "</td>";
  		echo "	<td>" . $suppl['uname'] . "(" . $suppl['per_unit'] . ")</td>";
  		echo "	<td class='bg'>" . $qty . "</td>";
  		echo "	<td>&#8369; " . number_format($suppl['cost'], 2, '.', ',') . "</td>";
  		echo "	<td class='bg'>" . number_format($suppl['cost'] * $qty, 2, '.', ',') . "</td>";
  		echo "</tr>";
		return $suppl['cost'] * $qty;
	}
}

if ($this->get('list') == 2){
    $is_get_item = $this->get('getitem');
	
    Connection::getConnect();
	if ($is_get_item)
	{
	   	$sdid = $this->get('id');
	    $sql = "SELECT *,(SELECT COUNT(rt.iid) FROM reorder_transaction rt JOIN reorder_response rr WHERE rt.iid = ii.iid AND rr.status=0) AS isorder FROM supplier_item_cost sic JOIN item_details id USING(iid) JOIN item_inventory ii USING(iid) WHERE (0=$sdid OR sic.sdid=$sdid) AND ii.reorder*per_unit >= ii.quantity GROUP BY id.name";
        Connection::setQuery($sql);
    	$result = Connection::fetchAll(MYSQL_ASSOC);
		foreach($result as $itm)
		{
			echo '<tr inprocess="0" supid="' . $itm['sdid'] . '" style="cursor:pointer;" sid="' . $itm['iid'] . '" class="' . ($itm['isorder']?'ordera':'') . '">';
			echo '<td class="ui-text-center bg" style="width:150px">' . $itm['name'] . '</td>';
			echo '<td class="ui-text-center" style="width:130px">' . $itm['quantity'] . '</td>';
			echo '</tr>';
		}
		exit;
	}
	else
	{
    	$id = $this->get('id');
		if ($id){
           	$sql = "SELECT * FROM supplier_item_cost si WHERE iid=$id";
            Connection::setQuery($sql);
        	$single = Connection::fetch();
		}
       	$sql = "SELECT * FROM supplier_details ORDER BY name";
        Connection::setQuery($sql);
    	$sup_suppliers = Connection::fetchAll(MYSQL_ASSOC);
	}
	
}

if( $this->get('getinfo')){
    Connection::getConnect();	
	$iid = $this->get('id');
	$sdid = $this->get('pid');
	$sql = "SELECT *, iud.name as uname, id.name as iname FROM supplier_item_cost sic JOIN item_details id USING(iid) JOIN item_inventory ii USING(iid) JOIN item_unit_detail iud USING(imdid) WHERE sic.iid = $iid AND sic.sdid=$sdid ORDER BY id.name";
    Connection::setQuery($sql);
    $result = Connection::fetch();
	
	echo "<input type='text' id='add_reorder_qty' value='" . $result['reorder2'] . "' class='ui-text-center' style='width: 60%;margin-top: -20px;margin-right: -5px;' />";
	echo "<input type='button' id='add_reorder' value='Add' style='width: 33% !important;float: right;margin-right: -10px;margin-top: -48px;' />";
	echo "<hr style='margin-bottom:10px;' />";
	
	echo "<div supid='" . $result['sdid'] . "' style='margin-right:-10px;height: 140px;margin-bottom: -15px;overflow: auto;line-height: initial;'><strong>Item Name:</strong>&emsp;<span>" . $result['iname'] . "</span><br />";
	echo "<strong>Cost&nbsp;:</strong>&emsp;<span>P " . $result['cost'] . "</span><br />";
	echo "<strong>Remaining Stock&nbsp;:</strong>&emsp;<span> " . floor($result['quantity']/$result['per_unit']) . ' ' . $result['uname'] . "(" . $result['per_unit'] . ")</span><br />";
	echo "<strong>Reorder Level&nbsp;:</strong>&emsp;<span> " . $result['reorder'] . "</span><br />";
	echo "<strong>Reorder Qty&nbsp;:</strong>&emsp;<span> " . $result['reorder2'] . "</span><br />";
	echo "<strong>Other Suppliers&nbsp;:</strong>&emsp;";
	$sql = "SELECT * FROM supplier_item_cost sic JOIN supplier_details sd USING(sdid) WHERE sic.iid = $iid";
    Connection::setQuery($sql);
    $results = Connection::fetchAll(MYSQL_ASSOC);
	$lsp = $sup_name = "";
	foreach($results as $x)
	{
		if( $x['sdid'] != $sdid )
			$lsp = $lsp . "<a href='#' class='supps' sup='" . $x['sdid'] . "'>" . $x['name'] . "</a>, ";
		else{
			$sup_name = $x['name'];
			$lsp = $lsp . "<em style='color:#1382B7'>" . $x['name'] . "</em>, ";
		}
	}
	echo trim($lsp," ,") . "</div>";
	echo "<div style='display:none;' id='reorder-temp' var='v" . $sdid . "_" . $iid . "' pid='" . $sdid . "' iid='" . $iid . "'>" . $sup_name . "," . $result['iname'] . "," . $result['uname'] . "</div>";
	exit;
}
?>
